package com.example.covid.service;

import com.example.covid.model.VirusData;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVRecord;
import org.springframework.core.io.Resource;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
import javax.annotation.PostConstruct;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;



@Service
public class serviceconfig {
    public static List<VirusData> value =new ArrayList<VirusData>();
    public static Integer Sum=0;
    public static Integer newcase=0;


    public List<VirusData> FindAll(){
        return value;
    }
    public VirusData findByProvince(String a){
        Pattern pattern=Pattern.compile(a,Pattern.CASE_INSENSITIVE);
        for(int i=0;i<value.size();i++){
            Matcher matcher=pattern.matcher(((VirusData)value.get(i)).getState());
            if(matcher.matches()){
                return value.get(i);
            }
        }
        return null;
    }
    public List<VirusData> findByCountry(String b){
        List<VirusData> list=new ArrayList<VirusData>();
        Pattern pattern=Pattern.compile(b,Pattern.CASE_INSENSITIVE);
        for(int i=0;i<value.size();i++){
            Matcher matcher=pattern.matcher(((VirusData)value.get(i)).getCountry());
            if(matcher.find()){
                list.add(value.get(i));
            }
        }
        return list;
    }

    @PostConstruct
    @Scheduled(cron = "${updateCron}")
    public void readService() throws IOException {
        RestTemplate template = new RestTemplate(new HttpComponentsClientHttpRequestFactory());
        String uriTemplate = "https://gitee.com/dgut-sai/COVID-19/raw/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv";
        URI uri = UriComponentsBuilder.fromUriString(uriTemplate).build(42);
        RequestEntity<Void> requestEntity = RequestEntity.get(uri)
                .headers(httpHeaders -> httpHeaders.add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3100.0 Safari/537.36"))
                .build();
        ResponseEntity<Resource> exchange = new RestTemplate().exchange(requestEntity, org.springframework.core.io.Resource.class);
        Resource body = exchange.getBody();

        Reader in = new InputStreamReader(body.getInputStream(), "UTF-8");
        Iterable<CSVRecord> records = CSVFormat.RFC4180.withFirstRecordAsHeader().parse(in);
        Sum = 0;
        newcase = 0;
        for (CSVRecord record : records) {
            Sum += Integer.parseInt(record.get(record.size() - 1));
            newcase += Integer.parseInt(record.get(record.size() - 1)) - Integer.parseInt(record.get(record.size() - 2));
            VirusData r = new VirusData(record.get("Province/State"), record.get("Country/Region"), Integer.parseInt(record.get(record.size() - 1)), Integer.parseInt(record.get(record.size() - 1)) - Integer.parseInt(record.get(record.size() - 2)));
            value.add(r);
        }
        System.out.println("success!");
    }
}
